package com.loong.service.impl;

import com.github.pagehelper.Page;
import com.github.pagehelper.PageHelper;
import com.loong.dto.StudentDTO;
import com.loong.entity.Student;
import com.loong.mapper.StudentMapper;
import com.loong.result.PageBean;
import com.loong.service.StudentService;
import com.loong.vo.StudentVO;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.util.List;

@Service
public class StudentServiceImpl implements StudentService {
    @Autowired
    private StudentMapper studentMapper;

    @Override
    public PageBean list(StudentDTO studentDTO) {
        PageHelper.startPage(studentDTO.getPage(), studentDTO.getPageSize());
        List<StudentVO> list = studentMapper.list(studentDTO.getName(), studentDTO.getDegree(), studentDTO.getClazzId());
        Page<StudentVO> studentPage = (Page<StudentVO>) list;
        return new PageBean(studentPage.getTotal(), studentPage.getResult());
    }

    @Override
    public List<Student> listAll() {
        return studentMapper.selectList(null);
    }

    @Override
    public Student get(Integer id) {
        return studentMapper.selectById(id);
    }

    @Override
    public void delete(List<Integer> ids) {
        studentMapper.deleteByIds(ids);
    }

    @Override
    public void insert(Student student) {
        studentMapper.insert(student);
    }

    @Override
    public void update(Student student) {
        studentMapper.updateById(student);
    }

    @Override
    public void violation(Integer id, Short score) {
        Student student = get(id);
        student.setViolationCount((short)(student.getViolationCount() + 1));
        student.setViolationScore((short) (student.getViolationScore() + score));
        studentMapper.updateById(student);
    }
}
